package com.sphere.freereader.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.sphere.freereader.R;
import com.sphere.freereader.entity.BookItem;
import com.sphere.freereader.utils.Helper;

import java.util.List;

/**
 * Created by aquarius on 2016/9/4.
 */
public class FreeReaderAdapter extends RecyclerView.Adapter<FreeReaderAdapter.ViewHolder>{

    private Context mContext;
    private LayoutInflater mInflater;
    private List<BookItem> bookItems;
    private ImageLoader imageLoader;

    public FreeReaderAdapter(Context context, List<BookItem> bookItems, ImageLoader imageLoader) {
        mContext = context;
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.bookItems = bookItems;
        this.imageLoader = imageLoader;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemview = mInflater.inflate(R.layout.book_category_item_layout, parent, false);
        return new ViewHolder(itemview);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Helper.loadImageForNetwork(holder.bookImgView, imageLoader, bookItems.get(position).imgUrl, R.drawable.img_no_book_cover);
        holder.bookImgView.setTag(bookItems.get(position).imgUrl);
        holder.bookNameTv.setText(bookItems.get(position).name);
    }

    @Override
    public int getItemCount() {
        return bookItems.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView bookImgView;
        TextView bookNameTv;

        public ViewHolder(final View itemView) {
            super(itemView);
            bookImgView = (ImageView) itemView.findViewById(R.id.image_view);
            bookNameTv = (TextView) itemView.findViewById(R.id.book_name_tv);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    android.util.Log.d("tag", "item =" + bookImgView.getTag()+", name="+bookNameTv.getText().toString());
                }
            });
        }
    }
}
